﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.Entities;
using MovieRental.SqlRepositoryLibrary.Converters;
using MovieRental.DataInterfaces;

namespace MovieRental.SqlRepositoryLibrary
{
    public class SqlCartDiscountRepository : SqlRepository<CartDiscountInfo, Guid, CartDiscountConverter>, ICartDiscountRepository
    {
        protected SqlDiscountRepository _baseRepository;

        public SqlCartDiscountRepository(string connectionString)
            : base(connectionString)
        {
            _baseRepository = new SqlDiscountRepository(_connectionString);
        }

        protected override bool Contains(CartDiscountInfo entity)
        {
            return Contains(entity.Id);
        }

        public override void Delete(CartDiscountInfo entity)
        {
            Delete(entity.Id);
        }

        public override void Save(CartDiscountInfo entity)
        {
            _baseRepository.Save(entity);
            base.Save(entity);
        }

        public override void Delete(Guid id)
        {
            base.Delete(id);
            if (_baseRepository.Contains(id))
                _baseRepository.Delete(id);
        }

        public override CartDiscountInfo Load(Guid id)
        {
            var discount = base.Load(id);
            discount.Value = _baseRepository.Load(id).Value;
            return discount;
        }

        public override IEnumerable<CartDiscountInfo> LoadAll()
        {
            var discounts = new List<CartDiscountInfo>(base.LoadAll());
            for (int i = 0; i < discounts.Count; i++)
                discounts[i].Value = _baseRepository.Load(discounts[i].Id).Value;
            return discounts;
        }
    }
}
